package com.itboye.mengwu.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.w3c.dom.Text;

import com.itboye.core.object.BaseActivity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.adapter.GridViewAdapter;
import com.itboye.mengwu.data.model.TestModel;
import com.itboye.mengwu.main.MatchGoods.GoodBeanInfo;
import com.itboye.mengwu.main.MatchGoods.GoodsBean;
import com.itboye.mengwu.widget.CarouselView;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;

public class MatchBuyActivity extends BaseActivity implements Observer {

	private List<ImageView> bmps;
	CarouselView topLunbo;
	int position;
	ListView listView;
	GridView gridView;

	TextView txt_title;
	ImageView back;

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub

	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_match_buy;
	}

	@Override
	public void onMyCreate() {
		txt_title.setText("搭配购");
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});
		bmps = new ArrayList<ImageView>();
		for (int i = 0; i < 3; i++) {
			position = i;
			ImageView imageView = new ImageView(this);
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setImageResource(R.drawable.ic_launcher);

			bmps.add(imageView);

		}
		topLunbo.setImageBitmaps(bmps);

		// MatchGoods matchGoods =new MatchGoods();
		// GoodsBean goodsBean = null;

		// ArrayList<GoodBeanInfo> goodBeanInfos = new
		// ArrayList<MatchGoods.GoodBeanInfo>();
		// for(int i=0;i<5;i++){
		// goodBeanInfos.add(goodBeanInfo);
		// }
		// goodsBean.setGoodBeanInfos(goodBeanInfos);
		// ArrayList<GoodsBean> goodsBeans =new
		// ArrayList<MatchGoods.GoodsBean>();
		// for(int i =0; i<5; i++){
		// goodsBeans.add(goodsBean);
		// }
		// matchGoods.setGoodsBeans(goodsBeans);

		MyAdapter myAdapter = new MyAdapter(this);
		listView.setAdapter(myAdapter);
	}

	public class MyAdapter extends BaseAdapter {

		// TextView goodsTitle,goodsValue;
		// ImageView goodsPic;

		MatchGoods matchGoods;
		Context context;

		public MyAdapter(Context context) {
			super();
			this.context = context;
		}

		public MyAdapter(MatchGoods matchGoods, Context context) {
			super();
			this.matchGoods = matchGoods;

		}

		@Override
		public int getCount() {
			return 5;
			// return (matchGoods != null &&
			// !matchGoods.getGoodsBeans().isEmpty()) ?
			// matchGoods.getGoodsBeans().size(): 0;
		}

		@Override
		public Object getItem(int position) {

			// return matchGoods.getGoodsBeans().get(position);
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MatchBuyListViewHolder holder = null;
			if (holder == null) {
				holder = new MatchBuyListViewHolder();
				convertView = LayoutInflater.from(MainApplication.getContext()).inflate(R.layout.item_matchbuy, null);
				holder.gridView = (GridView) convertView.findViewById(R.id.grid);
				convertView.setTag(holder);
			} else {
				holder = (MatchBuyListViewHolder) convertView.getTag();
			}

			MatchGoods.GoodsBean goodsBean = (GoodsBean) getItem(position);
			if (goodsBean != null) {

				getGridView(holder, goodsBean);

			}
			getGridView(holder, goodsBean);

			return convertView;
		}

		private void getGridView(MatchBuyListViewHolder holder, GoodsBean goodsBean) {

			int size = 5;

			int length = 100;

			DisplayMetrics dm = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(dm);
			float density = dm.density;
			int gridviewWidth = (int) (size * (length + 4) * density);
			int itemWidth = (int) (length * density);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth,
					LinearLayout.LayoutParams.FILL_PARENT);
			holder.gridView.setLayoutParams(params); // 重点
			holder.gridView.setColumnWidth(itemWidth); // 重点
			holder.gridView.setHorizontalSpacing(5); // 间距
			holder.gridView.setStretchMode(GridView.NO_STRETCH);
			holder.gridView.setNumColumns(size); // 重点

			// GridViewAdapter adapter = new
			// GridViewAdapter(getApplicationContext(),
			// cityList);
			// gridView.setAdapter(adapter);

			GridViewAdapter adapter = new GridViewAdapter();
			holder.gridView.setAdapter(adapter);

		}

		public class MatchBuyListViewHolder {
			GridView gridView;

		}

	}

	public class GridViewAdapter extends BaseAdapter {
		Context context;

		List<GoodBeanInfo> list;

		public GridViewAdapter() {
			super();
			// TODO Auto-generated constructor stub
		}

		public GridViewAdapter(Context _context, List<GoodBeanInfo> _list) {
			this.list = _list;
			this.context = _context;
		}

		@Override
		public int getCount() {
			// return list.size();
			return 5;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater layoutInflater = LayoutInflater.from(MainApplication.getContext());
			convertView = layoutInflater.inflate(R.layout.item_matchpay_recyclerview, null);
			TextView goodsTitle = (TextView) convertView.findViewById(R.id.goodsTitle);
			TextView goodsValue = (TextView) convertView.findViewById(R.id.goodsValue);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.ImageView);
			// goodsTitle.setText(list.get(position).getGoodsTitle());
			// goodsValue.setText(list.get(position).getGoodsValue());
			// imageView.setImageResource(list.get(position).getGoodsPic());
			goodsTitle.setText("鲍鱼熊掌");
			goodsValue.setText("998");
			imageView.setImageResource(R.drawable.ic_launcher);

			return convertView;
		}
	}

}
